<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <link href="../../libs/mui.min.css" rel="stylesheet"/>
    <link href="../../css/main.css" rel="stylesheet"/>
    <link rel="stylesheet" href="../../css/mine-collect.css"/>
</head>

<body>
<div class="head">
    <img id="back" class="mui-action-back" src="../../images/all-left.png" alt="">
    <p>我的收藏</p>
</div>
<div class="content">
    <p class="content-title">已收藏<span class="how"></span>条信息</p>
    <div class="content-t">
        <!--<div class="content-fi">
            <p>名校贷 学生贷</p>
            <p><span class="det">删除</span></p>
            <img src="../../images/helper-back.png" alt="" />
        </div>-->
    </div>

</div>

<script src="../../libs/mui.min.js"></script>
<script src="../../js/jquery-3.1.1.min.js"></script>
<script src="../../js/url.js"></script>
<script>
    var collectAjax = function () {
    };
    collectAjax.page = 1;
    collectAjax.getCollect = function () {
        $.ajax({
            url: ur + 'rest/productController/collect-list',
            type: 'GET',
            headers: {
                "X-Token": localStorage.token
            },
            dataType: 'json',
            data: {
                page: collectAjax.page
            },
        })
            .done(function (data) {
                if (data.meta.code === 'S002') {
                    $('.how').text(data.data.total);
                    var list = data.data.list;
                    var n = list.length;
                    var totalPage = Math.ceil(data.data.total / 10);
                    if (n > 0) {
                        for (var i = 0; i < n; i++) {
                            var div = $('<div class="content-fi"> ' +
                                '<p>' + list[i].product.name + '</p><p><span class="det" id=' +
                                list[i].id + '>删除</span></p>' +
                                '<img id="' + list[i].product.id + '" src="' + ur + list[i].product.image + '" alt=""/>' +
                                '</div>');
                            $('.content-t').append(div);
                        }
                        if (n < data.data.total && collectAjax.page < totalPage && $('.more').length == 0) {
                            $('.content-t').append('<p class="more">点击加载更多</p>');
                        }
                    } else if (n == 0) {
                        mui.toast('暂无收藏');
                    }
                } else {
                    mui.toast(data.meta.message);
                }
            })
    }
    collectAjax.getCollect();
    $('.content-t').on('tap', '.more', function () {
        //调用本身ajax;
        if (collectAjax.page < totalPage) {
            collectAjax.page++;
            collectAjax.getCollect();
        } else {
            $('.more').text('无更多收藏！');
        }
    })

    /*从收藏列表删除*/
    $(".content-t").on('tap', '.det', function () {
        var cli = $(this).attr('id');
        var all = $(this).parents('.content-fi')
        var btnArray = ['否', '是'];
        mui.confirm('确认删除，确认？', '小马时贷', btnArray, function (e) {
            if (e.index == 1) {
//						plus.nativeUI.showWaiting();
                $.ajax({
                    url: ur + 'rest/productController/del-collect',
                    type: 'POST',
                    dataType: 'json',
                    headers: {
                        "X-Token": localStorage.token
                    },
                    data: {
                        "id": cli,
                    }
                })
                    .done(function (data) {
//								plus.nativeUI.closeWaiting();
                        all.fadeOut(1000);
                        if (data.meta.code == 'S002') {
                            $('.how').text($('.how').text() - 1);
                            mui.toast('删除成功');
                        } else {
                            mui.toast(data.meta.message);
                        }
                    })
            }
        })
    });
    //跳转对应的收藏产品详情页面
    $('.content').on('tap', 'img', function () {
        localStorage.details = $(this).attr('id');
        href('../helper/details.html');
    })
</script>
</body>

</html>